module("luci.controller.nac.download", package.seeall)

function index()
local fs = require "nixio.fs"


entry({"admin","ac_nac","download"}, post("action_backup"))  

end

function ltn12_popen(command)                                                                                                                                                                                  
                                                                                                                                                                                                               
        local fdi, fdo = nixio.pipe()                                                                                                                                                                          
        local pid = nixio.fork()                                                                                                                                                                               
                                                                                                                                                                                                               
        if pid > 0 then                                                                                                                                                                                        
                fdo:close()                                                                                                                                                                                    
                local close                                                                                                                                                                                    
                return function()                                                                                                                  
                        local buffer = fdi:read(2048)                                                                                                                                                          
                        local wpid, stat = nixio.waitpid(pid, "nohang")                                                                            
                        if not close and wpid and stat == "exited" then                                                                            
                                close = true                                                                                                       
                        end                                                                                                                        
                                                                                                                                                                                                               
                        if buffer and #buffer > 0 then                                                                                             
                                return buffer                                                                                                      
                        elseif close then                                                                                                                                                                      
                                fdi:close()                                                                                                                                                                    
                                return nil                                                                                                         
                        end                                                                                                                        
                end                                                                                                
        elseif pid == 0 then                                                                                                                                                                                   
                nixio.dup(fdo, nixio.stdout)                                                   
                fdi:close()                                                                                     
                fdo:close()                                                                                                                        
                nixio.exec("/bin/sh", "-c", command)                                                                                                                                                           
        end                                                                                                                                                                                                    
end  






function action_backup()                                                                                                                                            
--  local reader = ltn12_popen("sysupgrade --create-backup - 2>/dev/null")
os.execute("/bin/cp -rf /usr/local/natshell/nac/upload_portal/html/ /tmp/html")
       local reader = ltn12_popen("tar -czO /tmp/html - 2>/dev/null")                                 
                                                                                                                                                                    
        luci.http.header(                                                                                                                                           
                'Content-Disposition', 'attachment; filename="portal.tar.gz"' %{                                        
                        luci.sys.hostname(),                                                                                                                        
                        os.date("%Y-%m-%d")                                                                                                                         
                })                                                                                                          
                                                                                                                                                                    
        luci.http.prepare_content("application/x-targz")                                                         
        luci.ltn12.pump.all(reader, luci.http.write)     
        os.execute("rm -rf /tmp/html")                                                                                                           
end  